package util;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * @author by zhangql
 * @Classname MapDistance
 * @Description TODO
 * @Date 2021/8/9 10:15
 */

public class MapDistance {


    private static double EARTH_RADIUS = 6378.137;

    private static double rad(double d) {
        return d * Math.PI / 180.0;
    }

    /**
     * 计算两点之间的距离
     *
     * @param depatureLng 出发地经度
     * @param depatureLat 出发地维度
     * @param arrivalLng  目的地经度
     * @param arrivalLat  目的地维度
     * @return 距离
     */
    public static double calculateDistance(double depatureLng, double depatureLat, double arrivalLng, double arrivalLat) {
        double lat1 = depatureLat;
        double lng1 =depatureLng;
        double lat2 = arrivalLng;
        double lng2 = arrivalLat;

        double radLat1 = rad(lat1);
        double radLat2 = rad(lat2);
        double difference = radLat1 - radLat2;
        double mdifference = rad(lng1) - rad(lng2);
        double distance = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(difference / 2), 2)
                + Math.cos(radLat1) * Math.cos(radLat2)
                * Math.pow(Math.sin(mdifference / 2), 2)));
        distance = distance * EARTH_RADIUS;
//        String distanceStr = distance + "";
//        distanceStr = distanceStr.
//                substring(0, distanceStr.indexOf("."));
        return BigDecimal.valueOf(Math.round(distance * 10000) / 10000).setScale(4,BigDecimal.ROUND_HALF_UP).doubleValue();
    }


}